package com.quanyue.music.user.mapper;

import com.quanyue.music.domain.entity.User;
import com.quanyue.music.user.type.UserDetail;
import com.quanyue.music.user.type.UserHowLong;
import com.shzisg.mybatis.mapper.auto.AutoMapper;
import com.shzisg.mybatis.mapper.page.Page;
import com.shzisg.mybatis.mapper.page.PageRequest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface UserMapper extends AutoMapper <User> {

    User findByName(@Param("nickName") String name);

    User findByOpenId(@Param("openId") String openId);

    void updateById(@Param("id") String userId, @Param("flow_type") Boolean isOpen);

    @Update("update qy_user set email = #{email} where id = #{id}")
    void updateEmail(@Param("email") String email, @Param("id") String userId);

    @Select("<script>select * from qy_user"
            + " <if test=\"nickName!=null\">where nick_name like CONCAT('%',#{nickName},'%')</if></script>")
    Page <User> findByMerchantNo(@Param("nickName") String name,
                                 @Param("request") PageRequest request);

    @Select("select u.*,v.end_time, count(o.user_id) as recharge_count, sum(o.order_price) as total_price from qy_user u "
            + "left join qy_fm_vip v on v.user_id=u.id "
            + "left join qy_order o on o.user_id=u.id and o.pay_id not in('NONE') "
            + "group by u.id")
    Page <UserDetail> findUserDetailPage(@Param("request") PageRequest request);

    @Select("select u.*,v.end_time, count(o.user_id) as recharge_count, sum(o.order_price) as total_price, r.valid_count "
            + "from qy_user u "
            + "left join qy_fm_vip v on v.user_id=u.id "
            + "left join qy_order o on o.user_id=u.id and o.pay_id not in('NONE') "
            + "left join qy_share_record r on r.issue_user=u.id "
            + "group by u.id ")
    List <UserDetail> findUserDetail();

    @Select("select r.user_id, SUM(TIMESTAMPDIFF(SECOND,r.create_time,r.modify_time)) as how_long "
            + "from qy_listen_record r "
            + "where DATE_FORMAT(r.create_time, '%Y-%m-%d') = #{date}"
            + "group by r.user_id ")
    List <UserHowLong> findUserHowLong(@Param("date") LocalDate date);
}
